package com.luoqiu.yygh.cmn.controller;

import com.luoqiu.yygh.cmn.service.DictService;
import com.luoqiu.yygh.common.result.Result;
import com.luoqiu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api("医院字典管理")
@RestController
@RequestMapping(value = "/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @GetMapping(value = "/{id}")
    public Result<Dict> getOne(@PathVariable Long id) {
        Dict dict = dictService.selectById(id);
        return Result.ok(dict);
    }

    @ApiOperation(value = "查询子数据")
    @GetMapping(value = "/findChildData/{id}")
    public Result<Object> findChildData(@PathVariable Long id) {
        Object o = dictService.findChildData(id);
        return Result.ok(o);
    }

    @GetMapping(value = "/export")
    public Result<Object> export(HttpServletResponse response) throws IOException {
        dictService.export(response);
        return Result.ok();
    }

    @PostMapping(value = "/import")
    public void importData(MultipartFile file) throws IOException {
        dictService.importData(file);
    }

    @GetMapping(value = "/getName/{dictCode}/{value}")
    public Result<String> getName(@PathVariable String dictCode, @PathVariable String value) {
        String name = dictService.getName(dictCode, value);
        return Result.ok(name);
    }

    @GetMapping(value = "/getName/{value}")
    public Result<String> getName(@PathVariable String value) {
        String name = dictService.getName(null, value);
        return Result.ok(name);
    }
}
